package com.waimai.mobile.ui.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.waimai.mobile.R;
import com.waimai.mobile.api.entity.Food;
import com.waimai.mobile.api.entity.OrderFood;
import com.waimai.mobile.api.entity.User;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;


/**
 * Created by lin on 2015/6/14.
 */

@EBean
public class FoodItemAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    @RootContext
    public Context context;

    private List<Food> foods;
    private List<OrderFood> orderFoods;

    private OnOrderFoodsChangeListener listener;

    public List<Food> getFoods() {
        return foods;
    }

    public void setFoods(List<Food> foods) {
        this.foods = foods;
        orderFoods = new ArrayList<OrderFood>();
        for(Food food : foods){
            OrderFood orderFood = new OrderFood(food.getFoodName(), food.getPrice(), 0);
            orderFoods.add(orderFood);
        }
    }

    public OnOrderFoodsChangeListener getListener() {
        return listener;
    }

    public void setListener(OnOrderFoodsChangeListener listener) {
        this.listener = listener;
    }

    @Override
    public View getHeaderView(int i, View view, ViewGroup viewGroup) {
        HeaderViewHolder holder;
        if(view == null){
            holder = new HeaderViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.header_item_list_food, null);
            holder.categoryNameTv = (TextView) view.findViewById(R.id.categoryNameTv);
            view.setTag(holder);
        }else{
            holder = (HeaderViewHolder) view.getTag();
        }

        holder.categoryNameTv.setText(foods.get(i).getCategory().getCategoryName());
        return view;
    }

    @Override
    public long getHeaderId(int i) {
        return (int) foods.get(i).getCategory().getCategoryId();
    }

    @Override
    public int getCount() {
        return foods.size();
    }

    @Override
    public Object getItem(int position) {
        return foods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_food, null);
            holder.foodNameTv = (TextView) convertView.findViewById(R.id.foodNameTv);
            holder.monthSalesTv = (TextView) convertView.findViewById(R.id.monthSalesTv);
            holder.zanTv = (TextView) convertView.findViewById(R.id.zanTv);
            holder.foodPriceTv = (TextView) convertView.findViewById(R.id.foodPriceTv);
            holder.foodMountTv = (TextView) convertView.findViewById(R.id.foodMountTv);
            holder.foodMountMinusIv = (ImageView) convertView.findViewById(R.id.foodMountMinusIv);
            holder.foodMountPlusIv = (ImageView) convertView.findViewById(R.id.foodMountPlusIv);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.foodNameTv.setText(foods.get(position).getFoodName());
        holder.monthSalesTv.setText(foods.get(position).getMonthSales()+"");
        holder.zanTv.setText(foods.get(position).getZan()+"");
        holder.foodPriceTv.setText(foods.get(position).getPrice()+"");

        holder.foodMountTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if("0".equals(s.toString())){
                    holder.foodMountMinusIv.setVisibility(View.GONE);
                    holder.foodMountTv.setVisibility(View.GONE);
                }else{
                    holder.foodMountMinusIv.setVisibility(View.VISIBLE);
                    holder.foodMountTv.setVisibility(View.VISIBLE);
                }

                if(listener != null){
                    listener.onOrderFoodsChanged(orderFoods);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        holder.foodMountMinusIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int foodAmount = orderFoods.get(position).getFoodAmount();
                --foodAmount;
                orderFoods.get(position).setFoodAmount(foodAmount);
                holder.foodMountTv.setText(foodAmount + "");
            }
        });
        holder.foodMountPlusIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int foodAmount = orderFoods.get(position).getFoodAmount();
                ++foodAmount;
                orderFoods.get(position).setFoodAmount(foodAmount);
                holder.foodMountTv.setText(foodAmount+"");
            }
        });

        return convertView;
    }

    public class HeaderViewHolder{
        TextView categoryNameTv;
    }

    public class ViewHolder{
        TextView foodNameTv;
        TextView monthSalesTv;
        TextView zanTv;
        TextView foodPriceTv;

        ImageView foodMountMinusIv;
        ImageView foodMountPlusIv;
        TextView foodMountTv;
    }

    public interface OnOrderFoodsChangeListener{
        public void onOrderFoodsChanged(List<OrderFood> orderFoods);
    }
}
